package utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MyTime {
    private String year; //2020
    private String year2;//20
    private String month;//03
    private String date;//10
    private String monthDate;//0310
    private String hour;
    private String minute;
    private String second;
    private String nyr;
    private String allTime;
    private long time;
    private String bmonthDate;
    private Date date2;

    public static void main(String[] args) {
        new MyTime("20200410085118");
    }
    public MyTime() {
        setAll(getBeforeDay(Calendar.getInstance(),0));
    }
    public MyTime(int i){
        setAll(getBeforeDay(Calendar.getInstance(),i));
    }
    public MyTime(String time){
        this.year = time.substring(0,4);
        this.year2 = time.substring(2,4);
        this.month = time.substring(4,6);
        this.date = time.substring(6,8);
        this.hour = time.substring(8,10);
        if(time.length()>10){
            this.minute = time.substring(10,12);
            this.second = time.substring(12,14);
        }else{
            this.minute = "00";
            this.second = "00";
        }

        this.monthDate = ""+this.month+this.date;
        this.nyr = this.month+"/"+this.date+"&nbsp"+this.hour+":"+minute;
        this.allTime = ""+this.year+this.monthDate+this.hour+this.minute+this.second;
        SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyyMMddHHmmss"); //
        try {
            Date parse = sDateFormat.parse(this.allTime);
            this.date2 = parse;
            this.time = parse.getTime();
            Calendar cal11 = Calendar.getInstance();

            cal11.setTime(parse);
            Calendar beforeDay = getBeforeDay(cal11, -1);
            String month1 = "00"+(beforeDay.get(Calendar.MONTH)+1);
            month1 = month1.substring(month1.length()-2);
            String date1 = "00"+beforeDay.get(Calendar.DATE);
            date1 = date1.substring(date1.length()-2);
            this.bmonthDate = ""+month1+date1;

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
    public void setAll(Calendar c){
        this.year = ""+c.get(Calendar.YEAR);
        this.year2 = year.substring(2,4);
        this.month = "00"+(c.get(Calendar.MONTH)+1);
        this.month = this.month.substring(this.month.length()-2);
        this.date = "00"+c.get(Calendar.DATE);
        this.date = this.date.substring(this.date.length()-2);
        this.hour = "00"+c.get(Calendar.HOUR_OF_DAY);
        this.hour = this.hour.substring(this.hour.length()-2);
        this.minute = "00"+c.get(Calendar.MINUTE);
        this.minute = this.minute.substring(this.minute.length()-2);
        this.second = "00"+c.get(Calendar.SECOND);
        this.second = this.second.substring(this.second.length()-2);

        this.monthDate = ""+this.month+this.date;
        this.nyr = this.month+"/"+this.date+"&nbsp"+this.hour+":"+minute;
        this.allTime = ""+this.year+this.monthDate+this.hour+this.minute+this.second;
        this.time = c.getTimeInMillis();
        SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyyMMddHHmmss"); //
        try {
            Date parse = sDateFormat.parse(this.allTime);
            this.date2 = parse;
            this.time = parse.getTime();
            Calendar cal11 = Calendar.getInstance();

            cal11.setTime(parse);
            Calendar beforeDay = getBeforeDay(cal11, -1);
            String month1 = "00"+(beforeDay.get(Calendar.MONTH)+1);
            month1 = month1.substring(month1.length()-2);
            String date1 = "00"+beforeDay.get(Calendar.DATE);
            date1 = date1.substring(date1.length()-2);
            this.bmonthDate = ""+month1+date1;

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    public Date getDate2() {
        return date2;
    }

    public void setDate2(Date date2) {
        this.date2 = date2;
    }

    public String getBmonthDate() {
        return bmonthDate;
    }

    public void setBmonthDate(String bmonthDate) {
        this.bmonthDate = bmonthDate;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public  Calendar setCalendar(int year, int month, int date){
        Calendar cl = Calendar.getInstance();
        cl.set(year, month-1, date);
        return cl;
    }
    public   Calendar getBeforeDay(Calendar cl,int i){
        //使用roll方法进行向前回滚
        //cl.roll(Calendar.DATE, -1);
        //使用set方法直接进行设置
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day+i);
        return cl;
    }

    public String getNyr() {
        return nyr;
    }

    public void setNyr(String nyr) {
        this.nyr = nyr;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getYear2() {
        return year2;
    }

    public void setYear2(String year2) {
        this.year2 = year2;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getMonthDate() {
        return monthDate;
    }

    public void setMonthDate(String monthDate) {
        this.monthDate = monthDate;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public String getMinute() {
        return minute;
    }

    public void setMinute(String minute) {
        this.minute = minute;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }

    public String getAllTime() {
        return allTime;
    }

    public void setAllTime(String allTime) {
        this.allTime = allTime;
    }

    @Override
    public String toString() {
        return "MyTime{" +
                "year='" + year + '\'' +
                ", year2='" + year2 + '\'' +
                ", month='" + month + '\'' +
                ", date='" + date + '\'' +
                ", monthDate='" + monthDate + '\'' +
                ", hour='" + hour + '\'' +
                ", minute='" + minute + '\'' +
                ", second='" + second + '\'' +
                '}';
    }
}
